<?php
namespace Models\Model;

use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\TableGateway;
use Zend\Db\Sql;
use Zend\Db\Sql\Where;
use Zend\Db\Sql\Select;
use Zend\Paginator\Adapter\DbSelect;
use Zend\Paginator\Paginator;
use Zend\Db\Sql\Expression;
class AnswersTable
{
    protected $tableGateway;
	protected $select;
    public function __construct(TableGateway $tableGateway)
    {
        $this->tableGateway = $tableGateway;
		$this->select = new Select();
    }
	
	public function saveAnswer($Question_id,$answer)
    {	
		$correct_answer="";
		if($answer['type']==0){
			$subjectId=0;
		}else{
		   $subjectId=$answer['subject_id'];
		}
		if($answer['correct_answer']=='option1'){
			$correct_answer='A';
		}else if($answer['correct_answer']=='option2'){
			$correct_answer='B';
		}else if($answer['correct_answer']=='option3'){
			$correct_answer='C';
		}else if($answer['correct_answer']=='option4'){
			$correct_answer='D';
		}
		$data = array(
			'question_id'			=> $Question_id,
			'subject_id'			=> $subjectId,
			'correct_answer' 	  	=> $correct_answer, 
			'level'					=> $answer['level'],
			'status' 				=> 1, 				
		);
		$this->tableGateway->insert($data);	
		return $this->tableGateway->lastInsertValue;
    }
	public function updateAnswer($answer)
    {	
		
		if($answer['type']==0){
			$subjectId=0;
		}else{
		   $subjectId=$answer['subject_id'];
		}
		$data = array(
			'question_id'			=> $answer['question_id'],
			'subject_id'			=> $subjectId,
			'correct_answer' 	  	=> $answer['correct_answer'], 
			'level'					=> $answer['level'],
			'status' 				=> 1, 				
		);
		$updatedData=$this->tableGateway->update($data, array('question_id' => $answer['question_id']));
		return 	$updatedData;
    }
	
	public function getQuestionslist(){
		$select = $this->tableGateway->getSql()->select();
		$select->where('status="1"');
		$resultSet = $this->tableGateway->selectWith($select);
		return $resultSet;
	}
}